import axios from 'axios'
import { config } from '@/config'
const { apiGateway } = config

axios.defaults.adapter = function (config) {
  return new Promise((resolve, reject) => {
    let data = config.method === 'get' ? config.params : config.data
    uni.request({
      url: config.url,
      method: config.method,
      data: data,
      header: config.headers,
      success: res => {
        return resolve(res)
      },
      fail: err => {
        return reject(err)
      }
    })
  })
}
export async function $axios ({ method, url, data, headers }) {
  try {
    const token = uni.getStorageSync('token') || 'null' 
    const response = await axios({
      url: apiGateway + url,
      method,
      headers: {
        'Content-Type': 'application/json',
        'token': token,
        ...headers
      },
      data
    })
    return response
  } catch (e) {
    console.error('$axios exception:', e)
  }
}
